
const app = getApp()
Page({
  data: {
    themeVal: app.globalData.theme,
    background: [
      '../../assets/images/banner03.jpg',
      '../../assets/images/banner02.png',
      '../../assets/images/banner04.jpg'
    ],
    themeBgObj: {
      themeA: '#ffeb0c',
      themeB: '#ff4500'
    },
    shopList: [],
    nearbyShop: [],
    buildingId: '',
    schoolName: '',
    shopType: '0',
    configState: true,
    configContent: '请选择学校',
    dialogBodyStyle: {
      width: '70%'
    }
  },
  onLoad: function (options) {
    if (JSON.stringify(options) != '{}') {
      this.setData({
        buildingId: options.buildingId,
        schoolName: decodeURIComponent(options.schoolName)
      })
      app.Store.set('indexShop', { buildingId: options.buildingId, schoolName: decodeURIComponent(options.schoolName) })

    }
    if (this.data.buildingId) {
      this.setData({
        configState: false
      })
    }
  },
  onShow: function () {
    app.Store.set("enterType", 'init')
    if (app.Store.get('indexShop')) {
      this.setData({
        buildingId: app.Store.get('indexShop').buildingId,
        schoolName: app.Store.get('indexShop').schoolName
      })
    }
    if (this.data.buildingId) {
      this.setData({
        configState: false
      })
    }
    this.getShop({ buildingId: this.data.buildingId, shopType: 0, type: 'init' })
    this.getShop({ buildingId: this.data.buildingId, shopType: 1, type: 'init' })
    this.getCarousel()
  },
  onPullDownRefresh: function () {//下来刷新
    wx.showNavigationBarLoading()
    if (this.data.buildingId) {
      this.getShop({ buildingId: this.data.buildingId, shopType: 0, type: 'pullDown' })
      this.getShop({ buildingId: this.data.buildingId, shopType: 1, type: 'pullDown' })
    } else {
      setTimeout(() => {
        // 隐藏导航栏加载框
        wx.hideNavigationBarLoading();
        // 停止下拉动作
        wx.stopPullDownRefresh();
      }, 10);
    }

  },
  close() {
    if (this.data.buildingId) {
      this.setData({
        configState: false
      })
    }
  },
  selectSchool() {
    if (this.data.buildingId) {
      this.setData({
        configState: false
      })
    }
    this.toSchool()
  },
  getCarousel() {
    app.Http.http({
      url: app.Api.QueryAd,
      method: 'POST',
      params: {}
    }).then(data => {
      this.setData({
        background: data
      })

    }).catch(err => {
      console.log(err)
    })
  },
  toSchool() {
    app.WxService.navigateTo('/pages/city-list/index', { type: 'index' })
  },
  goToStore(e) {
    let item = e.currentTarget.dataset.item;
    app.Store.set('shopDetail', {
      type: 'init',
      notice: item.notice,
      shopName: item.shopName,
      shopId: item.shopId,
      logo: item.logo,
      address: item.address,
      giveFree: item.giveFree,
      giveMoney: item.giveMoney,
      goodsTotal: item.goodsTotal,
      saleTotal: item.saleTotal,
      status: item.status,
      phone: item.mobile
    })
    app.Store.set('buyOrderType', 'init')
    app.WxService.navigateTo('/pages/shop/index/index')
  },
  getShop(objData) {
    if (objData.type === 'pullDown') {
      // 隐藏导航栏加载框
      wx.hideNavigationBarLoading();
      // 停止下拉动作
      wx.stopPullDownRefresh();
    }
    app.Http.http({
      url: app.Api.ShopList + '?buildingId=' + Number(objData.buildingId) + '&type=' + objData.shopType,
      method: 'POST',
      params: {}
    }).then(data => {
      if (objData.shopType == 1) {
        this.setData({ nearbyShop: data });
      } else {
        this.setData({ shopList: data });
      }

    }).catch(err => {
      console.log(err)
    })
  },
  toOpenShop() {//申请开店
    app.WxService.navigateTo('/pages/openShop/index')
  }
})